import {
  timestampToTime
} from '@/utils/formatTime'
import {
  ComponentWithStore
} from "mobx-miniprogram-bindings"
const computedBehavior = require('miniprogram-computed').behavior
import {
  reqOrderPrice,
} from '@/api/order'
import {
  toast
} from '@/utils/extendApi'
import verifier from '@/utils/verifier'

ComponentWithStore({
  behaviors: [computedBehavior],
  properties: {
    carStr: String
  },
  computed: {
    restOfWords(data) {
      return 200 - data.remark.length
    }
  },
  data: {
    car: {},
    showDate: false,
    showBigGoodsCounter: false,
    showRemark: false,
    remark: '',
    bigGoodsCount: 0,
    srcMoveWay: 0,
    srcFloorLevel: 1,
    descMoveWay: 0,
    descFloorLevel: 1,
    minDate: new Date().getTime(),
    order: {
      carTypeId: 0,
      srcLocation: '',
      descLocation: '',
      srcAddress: '',
      descAddress: '',
      srcFullAddress: '',
      descFullAddress: '',
      srcLatitude: '',
      srcLongitude: '',
      descLatitude: '',
      descLongitude: '',
      moveTime: '',
      needHelp: 0,
      bigGoodsCount: 0,
      remark: '',
      phone: '',
      srcMoveWay: 0,
      srcFloorLevel: 1,
      descMoveWay: 0,
      descFloorLevel: 1,
      transportPrice: 0,
      carryPrice: 0
    }
  },
  watch: {
    'order.srcAddress,order.srcLocation': function (srcAddress, srcLocation) {
      this.data.order.srcFullAddress = srcLocation + " " + srcAddress
    },
    'order.descAddress,order.descLocation': function (descAddress, descLocation) {
      this.data.order.descFullAddress = descLocation + " " + descAddress
    }
  },

  methods: {
    async back() {
      await wx.navigateBack()
      await wx.showTabBar()
    },
    onDisplayDatetime() {
      this.setData({
        showDate: true
      });
    },

    onChange({
      detail
    }) {
      this.setData({
        'order.needHelp': detail ? 1 : 0
      })
    },

    confirmSelectDate({
      detail
    }) {
      const res = timestampToTime(detail)
      this.setData({
        'order.moveTime': res
      })
      this.setData({
        showDate: false
      })
    },

    cancelSelectDate() {
      this.setData({
        showDate: false
      })
    },

    showBigGoodsCounter() {
      this.setData({
        showBigGoodsCounter: true
      })
    },

    changeBigGoodsCount(event) {
      this.setData({
        bigGoodsCount: event.detail
      })
    },

    confirmBigGoodsCount() {
      this.setData({
        'order.bigGoodsCount': this.data.bigGoodsCount,
        showBigGoodsCounter: false
      })
    },

    closeBigGoodsCounter() {
      this.setData({
        showBigGoodsCounter: false,
        bigGoodsCount: this.data.order.bigGoodsCount
      })
    },

    showRemark() {
      this.setData({
        showRemark: true
      })
    },

    closeRemark() {
      this.setData({
        showRemark: false,
        remark: this.data.order.remark
      })
    },

    confirmRemark() {
      this.setData({
        showRemark: false,
        'order.remark': this.data.remark
      })
    },

    clearRemark() {
      this.setData({
        remark: ''
      })
    },

    async chooseSrcAddress() {
      const res = await wx.chooseLocation()
      console.log('地理位置',res)
      this.setData({
        'order.srcLocation': res.address,
        'order.srcLatitude': res.latitude + '',
        'order.srcLongitude': res.longitude + '',
      })
    },

    inputSrcAddress(event) {
      this.setData({
        'order.srcAddress': event.detail.value,
      })
    },
    async chooseDescAddress() {
      const res = await wx.chooseLocation()
      this.setData({
        'order.descLocation': res.address,
        'order.descLatitude': res.latitude + '',
        'order.descLongitude': res.longitude + ''
      })
    },
    inputDescAddress(event) {
      this.setData({
        'order.descAddress': event.detail.value,
      })
    },

    showSrcMoveWay() {
      this.setData({
        showSrcMoveWay: true
      })
    },

    cancelSrcMoveWay() {
      this.setData({
        showSrcMoveWay: false,
        srcMoveWay: this.data.order.srcMoveWay,
        srcFloorLevel: this.data.order.srcFloorLevel
      })
    },

    changeSrcMoveWay(event) {
      this.setData({
        srcMoveWay: event.detail
      })
    },


    confirmSrcMoveWay() {
      this.setData({
        showSrcMoveWay: false,
        'order.srcMoveWay': this.data.srcMoveWay,
        'order.srcFloorLevel': this.data.srcFloorLevel
      })
    },

    showDescMoveWay() {
      this.setData({
        showDescMoveWay: true
      })
    },

    cancelDescMoveWay() {
      this.setData({
        showDescMoveWay: false,
        descMoveWay: this.data.order.descMoveWay,
        descFloorLevel: this.data.order.descFloorLevel
      })
    },

    changeDescMoveWay(event) {
      this.setData({
        descMoveWay: event.detail
      })
    },

    confirmDescMoveWay() {
      this.setData({
        showDescMoveWay: false,
        'order.descMoveWay': this.data.descMoveWay,
        'order.descFloorLevel': this.data.descFloorLevel
      })
    },
    inputPhone(event) {
      this.setData({
        'order.phone': event.detail.value
      })
    },
    async calcPrice() {
      if (!this.data.order.srcLocation) {
        toast({
          title: '请先进行起始地址定位~~'
        })
        return
      }
      if (!this.data.order.srcAddress) {
        toast({
          title: '请填写起始地址详细信息~~'
        })
        return
      }
      if (!this.data.order.descLocation) {
        toast({
          title: '请先进行目的地址定位~~'
        })
        return
      }
      if (!this.data.order.descAddress) {
        toast({
          title: '请填写目的地址详细信息~~'
        })
        return
      }
      if (!this.data.order.moveTime) {
        toast({
          title: '请填写搬运时间~~'
        })
        return
      }
      const success = this.verifyPhone()
      if (!success) {
        return
      }
      // 向后端发送计算价格的请求，返回价格
      const res = await reqOrderPrice(this.data.order)
      this.data.order.carryPrice = Math.floor(res.data.carryPrice * 100) / 100
      this.data.order.transportPrice = Math.floor(res.data.transportPrice * 100) / 100
      wx.navigateTo({
        url: `/pages/temp-order/temp-order?tempOrderStr=${JSON.stringify(this.data.order)}`,
      })
    },
    verifyPhone() {
      if (!this.data.order.phone) {
        toast({
          title: '请填写联系电话'
        })
        return false;
      }
      if (!verifier.verifyPhone(this.data.order.phone)) {
        toast({
          title: '手机号格式错误'
        })
        return false;
      }
      return true;
    },
  },
  lifetimes: {
    attached() {
      const carObj = JSON.parse(this.properties.carStr)
      this.setData({
        car: carObj,
        'order.carTypeId': carObj.id,
        'order.carTypeName': carObj.name
      })
    }
  }
})